package com.zhigh.project.security.jwt.authentication;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * sub: Subject(主体)
 * iss: issuer(Token签发者)
 * aud: audience(Token受众)
 * exp: expirationTime(Token过期时间)
 * nbf: notBeforeTime(Token生效开始时间)
 * iat: issueTime(Token签发时间)
 * jti: jwtID(jwt id)
 */
public interface JWTPayload {
    String getSub();

    String getIss();

    List<String> getAud();

    Long getExp();

    Long getNbf();

    Long getIat();

    String getJti();

    String getUsername();

    List<String> getAuthorities();

    Map<String, Object> getAdditional();

    String toPayloadStr();


    default Optional<Object> getObjFromAdditionalObj(String key) {
        return Optional.ofNullable(getAdditional()).map(add -> add.get(key));
    }

    default Optional<Long> getLongFromAdditional(String key) {
        return getObjFromAdditionalObj(key).map(Long.class::cast);
    }

    default Optional<Integer> getIntegerFromAdditional(String key) {
        return getObjFromAdditionalObj(key).map(Integer.class::cast);
    }
}
